home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_asynchat.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  2KB  |  71 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import thread
  5. import asyncore
  6. import asynchat
  7. import socket
  8. import threading
  9. import time
  10. HOST = '127.0.0.1'
  11. PORT = 54321
  12.  
  13. class echo_server(threading.Thread):
  14.     
  15.     def run(self):
  16.         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  17.         sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  18.         sock.bind((HOST, PORT))
  19.         sock.listen(1)
  20.         (conn, client) = sock.accept()
  21.         buffer = ''
  22.         while '\n' not in buffer:
  23.             data = conn.recv(10)
  24.             if not data:
  25.                 break
  26.             
  27.             buffer = buffer + data
  28.         while buffer:
  29.             n = conn.send(buffer)
  30.             buffer = buffer[n:]
  31.         conn.close()
  32.         sock.close()
  33.  
  34.  
  35.  
  36. class echo_client(asynchat.async_chat):
  37.     
  38.     def __init__(self):
  39.         asynchat.async_chat.__init__(self)
  40.         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
  41.         self.connect((HOST, PORT))
  42.         self.set_terminator('\n')
  43.         self.buffer = ''
  44.  
  45.     
  46.     def handle_connect(self):
  47.         print 'Connected'
  48.  
  49.     
  50.     def collect_incoming_data(self, data):
  51.         self.buffer = self.buffer + data
  52.  
  53.     
  54.     def found_terminator(self):
  55.         print 'Received:', repr(self.buffer)
  56.         self.buffer = ''
  57.         self.close()
  58.  
  59.  
  60.  
  61. def main():
  62.     s = echo_server()
  63.     s.start()
  64.     time.sleep(1)
  65.     c = echo_client()
  66.     c.push('hello ')
  67.     c.push('world\n')
  68.     asyncore.loop()
  69.  
  70. main()
  71.